using System;
using UnityEngine;

namespace Jerry.UiFrame
{
    public static class UIConfig
    {
        private delegate JLoader JLoaderDelegate();

        private static JLoaderDelegate _loader;

        /// <summary>
        /// 最大存在界面数
        /// </summary>
        public static int MaxExistWindows { set; get; } = 10;

        /// <summary>
        /// 通用按钮点击音效路径
        /// </summary>
        public static string BtnClickSoundPath
        {
            set => BtnClickSound = GetLoader().LoadExternal<AudioClip>(value);
        }
        
        internal static AudioClip BtnClickSound;
        
        public static void SetLoaderExtension(Type type)
        {
            _loader = () => Activator.CreateInstance(type) as JLoader;
        }

        /// <summary>
        /// 设置UI渲染Camera
        /// </summary>
        public static Camera UiCamera
        {
            set => UIManager.UICamera = value;
            get => UIManager.UICamera;
        }
        
        /// <summary>
        /// 设置遮罩颜色
        /// </summary>
        public static Color UiMaskColor = new Color(1, 1, 1, 0.4f);
        
        /// <summary>
        /// 设置UI音效的大小
        /// </summary>
        public static float UiAudioVolume
        {
            set
            {
                if (UIManager.AudioSource)
                    UIManager.AudioSource.volume = value;
            }
        }
        
        /// <summary>
        /// 获取UI的Canvas
        /// </summary>
        /// <returns></returns>
        internal static Canvas GetCanvas()
        {
            return UIManager.Canvas;
        }

        /// <summary>
        /// 获取播放UI音效的AudioSource
        /// </summary>
        /// <returns></returns>
        internal static AudioSource GetAudioSource()
        {
            return UIManager.AudioSource;
        }
        
        /// <summary>
        /// 获取加载器
        /// </summary>
        /// <returns></returns>
        internal static JLoader GetLoader()
        {
            if(_loader == null)
                return new JLoader();
            return _loader();
        }
    }
}